package com.gombe.credit.app.ui.adapter

import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.gombe.credit.app.R
import com.gombe.credit.app.utils.GombeCreditImageLoader
import com.gombe.credit.lib.adapter.GombeCreditRecyclerViewAdapter
import com.gombe.credit.lib.adapter.GombeCreditViewHolderHelper
import com.gombe.credit.lib.shapeview.layout.GombeCreditConstraintLayout
import org.json.JSONObject

/**
 * GombeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/21 12:02
 * @Annotation：绑卡适配器
 */
class AdapterGombeCreditStepWallet(recyclerView: RecyclerView?): GombeCreditRecyclerViewAdapter<JSONObject>(recyclerView) {

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        if (model.optBoolean("isEdit",false)){
            return R.layout.fragment_gombe_credit_step_wallet_edit_item
        }
        return R.layout.fragment_gombe_credit_step_wallet_index_item
    }

    override fun setItemChildListener(helper: GombeCreditViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.walletEdit) //点击编辑
        helper.setItemChildClickListener(R.id.walletDelete)//删除
    }

    override fun fillData(helper: GombeCreditViewHolderHelper, position: Int, model: JSONObject) {
        if (position == 0){
            val showText = "*${String.format(mContext.getString(R.string.gombe_credit_process_step_wallet_index),position+1)}"
            val spString = SpannableString(showText)
            spString.setSpan(ForegroundColorSpan(getColor(R.color.gombe_credit_FFFF4D3E)), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            helper.setText(R.id.walletIndex,spString)
        } else {
            helper.setText(R.id.walletIndex,String.format(mContext.getString(R.string.gombe_credit_process_step_wallet_index),position+1))
        }

        //是否是编辑
        val isEdit = model.optBoolean("isEdit",true)
        if (!isEdit){
            val walletDesc = model.optString("walletDesc")
            helper.setText(R.id.walletTypeTitle, walletDesc)
            helper.setText(R.id.walletAccountMobile,"+" + model.optString("walletMobile"))

            val walletTypeLogo = helper.getImageView(R.id.walletTypeLogo)
            GombeCreditImageLoader.instance?.loadImageUrl(mContext,walletTypeLogo,model.optString("walletLogo"), R.mipmap.gombe_credit_image_holder)

            val itemLayout = helper.getView<GombeCreditConstraintLayout>(R.id.itemLayout)
            if (walletDesc.uppercase().startsWith("W")){
                itemLayout.shapeDrawableBuilder
                    .setSolidColor(getColor(R.color.gombe_credit_FFF1FCFF))
                    .setStrokeColor(getColor(R.color.gombe_credit_FFDCF7FF))
                    .intoBackground()
            } else {
                itemLayout.shapeDrawableBuilder
                    .setSolidColor(getColor(R.color.gombe_credit_FFFFF6F1))
                    .setStrokeColor(getColor(R.color.gombe_credit_FFFFE9DC))
                    .intoBackground()
            }

            //是否显示 删除按钮
            if (model.has("isShowDelete") && !model.optBoolean("isShowDelete")){
                helper.setVisibility(R.id.walletDelete, View.INVISIBLE)
            } else {
                helper.setVisibility(R.id.walletDelete, View.VISIBLE)
            }
        }
    }
}